package com.guhappy.stockutil;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StockPriceHistr implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private Date history_date;
	private Float history_open;
	private Float history_close;
	private Float high_price;
	private Float low_price;
	private Long volume;
	
	private GlobalStock stock;

	/**
	 * @param id
	 * @param history_date
	 * @param history_open
	 * @param history_close
	 */
	public StockPriceHistr(String id, Date history_date, Float history_open,
			Float history_close, Float high_price, Float low_price, Long volume) {
		this.id = id;
		this.history_date = history_date;
		this.history_open = history_open;
		this.history_close = history_close;
		this.high_price = high_price;
		this.low_price = low_price;
		this.volume = volume;
	}
	
	// for bulk insert
	public StockPriceHistr(Object[] ob) throws Exception {
		DateFormat formatter = new SimpleDateFormat("dd-MM-yy");
		DateFormat formatter2 = new SimpleDateFormat("MM/dd/yy");
		
		if (isDateStringValid((String) ob[0], formatter)) {
			java.util.Date datetime = null;
			datetime = formatter.parse((String) ob[0]);
			this.history_date = datetime;
		} else if (isDateStringValid((String) ob[0], formatter2)) {
			java.util.Date datetime = null;
			datetime = formatter2.parse((String) ob[0]);
			this.history_date = datetime;
		}
		
		this.history_open = Float.parseFloat((String) ob[1]);
		this.history_close = Float.parseFloat((String) ob[2]);
		this.high_price = Float.parseFloat((String) ob[3]);
		this.low_price = Float.parseFloat((String) ob[4]);
        if (ob[5]!=null && !((String) ob[5]).trim().equals("")&& !((String) ob[5]).trim().equals("N/A")){
        	this.volume = Long.parseLong((String) ob[5]);
        }
	}
	
	public boolean isDateStringValid(String date, DateFormat dateFormat) {
		try {
			dateFormat.format(dateFormat.parse(date));
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	public StockPriceHistr() {
		// TODO Auto-generated constructor stub
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Float getHigh_price() {
		return high_price;
	}

	public void setHigh_price(Float high_price) {
		this.high_price = high_price;
	}

	public Float getLow_price() {
		return low_price;
	}

	public void setLow_price(Float low_price) {
		this.low_price = low_price;
	}

	public Long getVolume() {
		return volume;
	}

	public void setVolume(Long volume) {
		this.volume = volume;
	}

	public Date getHistory_date() {
		return history_date;
	}

	public void setHistory_date(Date history_date) {
		this.history_date = history_date;
	}

	public Float getHistory_open() {
		return history_open;
	}

	public void setHistory_open(Float history_open) {
		this.history_open = history_open;
	}

	public Float getHistory_close() {
		return history_close;
	}

	public void setHistory_close(Float history_close) {
		this.history_close = history_close;
	}

	public GlobalStock getStock() {
		return stock;
	}

	public void setStock(GlobalStock stock) {
		this.stock = stock;
	}
	
}
